import Vue from 'vue';
import Vuex from 'vuex';
import request from '../common/request';
import i18n from "@/language/index18.js";
Vue.use(Vuex);
const store = new Vuex.Store({
	state: {
		user: {},
		hasLogin: false,
		tabbar: [
		    {
				iconPath: "/static/tabbar/1.png",
		        selectedIconPath: "/static/tabbar/2.png",
		        text: i18n.t("首页"),
		        customIcon: false,
				path: "/pages/index/index"
		    },
		    {
		        iconPath: "/static/tabbar/3.png",
		        selectedIconPath: "/static/tabbar/4.png",
		        text: i18n.t("市场"),
		        customIcon: false,
				path: "/pages/market/index"
		    },
		    {
		        iconPath: "/static/tabbar/5.png",
		        selectedIconPath: "/static/tabbar/6.png",
		        text: i18n.t("质押"),
				customIcon: false,
				path: "/pages/pledge/index"
		    },
			{
			    iconPath: "/static/tabbar/7.png",
			    selectedIconPath: "/static/tabbar/8.png",
			    text: i18n.t("推广"),
				customIcon: false,
				path: "/pages/promotion/index"
			},
			{
			    iconPath: "/static/tabbar/9.png",
			    selectedIconPath: "/static/tabbar/10.png",
				text: i18n.t("我的"),
				customIcon: false,
				path: "/pages/person/index"
			},
		],
		tabbarCurrent: 0,
	},
	// 修改store中的值唯一的方法就是提交mutation来修改
	mutations: {
		/**
		 * 设置状态
		 * @param {Object} state 状态对象
		 * @param {Object} provider 传入值
		 */
		userState(state, provider) {
			state.user = provider
		},
		setTabbarCurrent(state, tabbarCurrent){
			state.tabbarCurrent = tabbarCurrent
		},
		hasLoginState(state, provider){
			state.hasLogin = provider
		},
	},
	actions: {
		// 用户信息
		getUser(context){
			request("/api/v3/user/info", "GET", {}).then(res=>{
				if(res.code==0) {
					console.log("用户信息", res.data)
					uni.setStorageSync("user", res.data)
					context.commit("userState", res.data)
				}
			})
		},
		
		setUser(context, provider){
			context.commit("userState", provider)
		},
		
		setHasLogin(context, provider){
			context.commit("hasLoginState", provider)
		},
		
		// tabbar切换
		getTabbarCurrent(context, tabbarCurrent){
			if(context.state.tabbarCurrent==tabbarCurrent) return
			context.commit("setTabbarCurrent", tabbarCurrent)
			uni.switchTab({
				url: context.state.tabbar[tabbarCurrent].path
			})
		},
	}
})

export default store